<?php

use Drupal\shusy_team_achievement\Entity\ShusyTeamAchievement;

function shusy_team_achievement_shusy_team_achievement_entity_form($form, &$form_state, ShusyTeamAchievement $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_team_achievement') . '/shusy_team_achievement.forms.inc';
  $form_state[ShusyTeamAchievement::class] = $e;

  $form['achievement'] = [
    '#type' => 'textfield',
    '#title' => t('业绩'),
    '#required' => TRUE,
    '#default_value' => $e->achievement / 100,
    '#element_validate' => ['element_validate_number'],
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_team_achievement_shusy_team_achievement_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyTeamAchievement::class];
  if ($e instanceof ShusyTeamAchievement) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->achievement = ceil($e->achievement * 100);
    $e->save();
  }
}

function shusy_team_achievement_shusy_team_achievement_delete_form($form, &$form_state, ShusyTeamAchievement $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_team_achievement') . '/shusy_team_achievement.forms.inc';
  $form_state[ShusyTeamAchievement::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_team_achievement/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_team_achievement_shusy_team_achievement_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyTeamAchievement::class];
  if ($e instanceof ShusyTeamAchievement) {
    $e->delete();
  }
}

